package com.sl.leetcode;

import org.junit.Test;

import java.util.HashMap;
import java.util.Map;

/**
 * Created by lisulong on 2019/5/6.
 */
public class Solution {
    //    public static void main(String[] args) {
//        Solution solution=new Solution();
//        int[] nums = {3,2,4};
//        int target = 6;
//        System.out.println(solution.twoSum2(nums,target)[0]);
//        System.out.println(solution.twoSum2(nums,target)[1]);
//    }
    //7. 整数反转
    //2147483647
    //-2147483648
    //当x=-2147483648时，-x也等于-2147483648
    //☆☆☆int相乘的时候，如果溢出，返回错误结果，而不是报错
    public static void main(String[] args) {
        System.out.println(reverse5(1534236469));
//        System.out.println(reverse(1534236469));
//        System.out.println(reverse(153423646));
//        System.out.println(reverse(-1230));
//        System.out.println(Integer.toBinaryString(Integer.MAX_VALUE));
//        System.out.println(Integer.toBinaryString(Integer.MAX_VALUE));
//        System.out.println(Long.toBinaryString(Integer.MAX_VALUE + 1));
//        System.out.println(Long.toBinaryString(Integer.MAX_VALUE));
//        Long i = Integer.MAX_VALUE + 12L;
//        System.out.println(i);
//        System.out.println(i.intValue());
    }

    @Test
    public void test1() {
        int n = 2;

        // returns the value obtained by rotating left
        for (int i = 0; i < 4; i++) {
            n = Integer.rotateLeft(n, 4);
            System.out.println(n);
        }

    }

    public static int reverse(int x) {
        //if (negative) x = -x;
        boolean negative = x < 0;
        String xStr = new StringBuilder(Integer.toString(x).replace("-", "")).reverse().toString();
        Long l;
        Long intMaxLong = Integer.MAX_VALUE + 0L;
        Long intNegativeMaxLong = 2147483648L;
        l = Long.parseLong(xStr);
        if ((negative && l > intNegativeMaxLong) || (!negative && l > intMaxLong))
            return 0;
        return l.intValue() * (negative ? -1 : 1);
    }

    //官方解答；6ms
    public static int reverse2(int x) {
        int rev = 0;
        while (x != 0) {
            int pop = x % 10;
            x /= 10;
            if (rev > Integer.MAX_VALUE / 10 || (rev == Integer.MAX_VALUE / 10 && pop > 7)) return 0;
            if (rev < Integer.MIN_VALUE / 10 || (rev == Integer.MIN_VALUE / 10 && pop < -8)) return 0;
            rev = rev * 10 + pop;
        }
        return rev;
    }

    //29ms
    public static int reverse3(int x) {
        boolean negative = x < 0;
        String xStr = new StringBuilder(Integer.toString(x).replace("-", "")).reverse().toString();
        try {
            return Integer.parseInt(xStr) * (negative ? -1 : 1);
        } catch (Exception e) {
            return 0;
        }
    }

    //6ms
    public static int reverse4(int x) {
        int[] nums = new int[10];
        int temp = x;
        int cursor = 0;
        for (int i = 0; i < 10; i++) {
            nums[i] = temp % 10;
            temp = temp / 10;
            if (temp == 0) {
                break;
            }
            cursor++;
        }
        x = 0;
        int last;
        for (int i = 0; i < cursor + 1; i++) {
            last = x;
            x = x * 10 + nums[i]; //把上面的式子反过来算看和原来的数是否相同，不相同说明溢出了 
            if ((x - nums[i]) / 10 != last) {
                return 0;
            }
        }
        return x;
    }
    //5 ms
    public static int reverse5(int x) {
        int rev = 0;int last;        
        while (x != 0) {
            last = rev;
            int pop = x % 10;
            x /= 10;            
            rev = rev * 10 + pop;
            if ((rev - pop) / 10 != last) {
                return 0;
            }
        }
        return rev;
    }
    public int[] twoSum(int[] nums, int target) {
        int[] result = new int[2];
        for (int i = 0; i < nums.length; i++) {
            for (int j = i + 1; j < nums.length; j++) {
                if (nums[i] + nums[j] == target) {
                    result[0] = i;
                    result[1] = j;
                }
            }
        }
        return result;
    }

    public int[] twoSum2(int[] nums, int target) {
        Map<Integer, Integer> map = new HashMap<>();
        for (int i = 0; i < nums.length; i++) {
            map.put(nums[i], i);
            int complement = target - nums[i];
            if (map.containsKey(complement)) {
                return new int[]{map.get(complement), i};
            }
        }
        throw new IllegalArgumentException("No two sum solution");
    }


}
